/*
 * @Author: lerio.cheng
 * @Date:   2017-02-22 16:44:17
 * @Last Modified by:   DELL
 * @Last Modified time: 2017-02-24 12:49:10
 */

'use strict';

app.controller('modelPicsListCtrl', function($scope, tipService, $compile, $http, $window, rest, $state, ngDialog, user, $location,uploadConf,mapSev,uiLoad) {

    var tmpHouseIdArr = "";

    function time(mydate){
      var str = mydate.substring(0,10);
      var dataArr = str.split('-');
      var timeStr = dataArr[0]
        +"年"+dataArr[1]+"月"+dataArr[2]
        +"日";
        return timeStr;
    }

    $scope.dataTableSettings = {
        "sAjaxSource": "3014",
        "aoColumns": [
            {
                "mDataProp": "name",
                "sTitle": "房型图片模板名称",
                "sDefaultContent": "",
                "sClass": "center"
            }, {
                "mDataProp": "createTime",
                "sTitle": "创建时间",
                "sDefaultContent": "",
                "sClass": "center"
            }, {
                "mDataProp": "operate",
                "sTitle": "操作",
                "sDefaultContent": "",
                "sClass": "center handle-td"
            }
        ],
        "fnRowCallback": function(nRow, aData, iDisplayIndex) {
            var btnStr = "";
            
            var buttons = getButtons(aData);

            //var btnStr = buttons.join("");

            $(nRow).find(".handle-td").append($compile(buttons)($scope));
            $(nRow).find(".handle-td").prev().empty().append(time(aData.createTime));

            tmpHouseIdArr += aData.houseId + ",";
            if (iDisplayIndex >= 9) {
                // console.log(tmpHouseIdArr);
            }
        },
        "fnDrawCallback": function(oSettings) {
            
        },
        "fnServerParams": function(aoData) {
            aoData.push({
                'name': 'queryParams',
                'value': $scope.queryParams
            });
            return aoData;
        }
    }

    $scope.initFinish = function(dataTable) {
         $scope.dataTable = dataTable;
     }

    var btntmp = function (opt) {
        var wrap = document.createElement('div'), abtn = document.createElement('a');
        wrap.appendChild(abtn);
        $(abtn).attr('href', 'javascript:;').addClass('btn');
        if (opt.isDisabled) {
            $(abtn).addClass('disabled');
        }
        $(abtn).text(opt.buttonText);
        if (opt.functionName) {
            var ngClick = opt.functionName + '($event, "'+opt.paramId+'")';
            $(abtn).attr('ng-click', ngClick);
        }
        return $(wrap).html();
    }

    var getButtons = function (opt) {

        var editTempletBtn = function (opt) { // 编辑
            return btntmp({
                isDisabled: false,
                paramId: opt.id,
                functionName: "editHouseTemplet",
                buttonText: "编辑"
            });
        }

        return editTempletBtn(opt);
    }

    $scope.queryUser = function(){ 
        $scope.queryParams = {
            'name': $scope.name
        }
        console.log($scope.queryParams);
        $scope.dataTable.fnClearTable();
        // $scope.queryParams = {};       

    };

    $scope.editHouseTemplet = function(e, templetId){
        $window.localStorage.setItem('templetId',JSON.stringify({id:templetId}));
        $state.go('app.houseOwner.editHousePicsTemplet',{id:templetId});
    }

    $scope.addModelPics = function () {
        $state.go('app.houseOwner.housePicsTemplet');
    }
});

